package com.tiyende.kash.loop.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.common.CommonActivity
import com.tiyende.kash.loop.configs.TiyendeConfigs
import com.tiyende.kash.loop.databinding.TiyendeActivityWebviewBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType


/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:17
 * @Annotation：WebView
 */
class TiyendeWebviewActivity : CommonActivity() {
    private lateinit var binding: TiyendeActivityWebviewBinding

    companion object {
        private const val INTENT_LINK_URL = "linkUrl"
        private const val INTENT_LINK_TITLE = "linkTitle"
        fun start(context: Context, linkTitle: String, linkUrl: String) {
            val intent = Intent(context, TiyendeWebviewActivity::class.java)
            intent.putExtra(INTENT_LINK_URL, linkUrl)
            intent.putExtra(INTENT_LINK_TITLE, linkTitle)
            context.startActivity(intent)
        }
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeActivityWebviewBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        setTiyendeTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setTiyendeTextTitle(getWebViewTitle())
        setTiyendeTextTitleGravity(Gravity.CENTER)
        setTiyendeBackBtnRes(R.mipmap.tiyende_back_black)

        initWebViewSettings()
    }

    override fun initTiyendeListener() {
        //联系客服
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("${getWebViewTitle()} Page")
            addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_CUSTOMER,"${getWebViewTitle()} Page")
        }
    }

    override fun loadTiyendeData() {
        binding.webView.loadUrl(getWebViewUrl())
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.webView.setInitialScale(100)
        binding.webView.webChromeClient = mWebChromeClient
        binding.webView.webViewClient = mWebClient
        val webSettings = binding.webView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.webLoading.progress = newProgress
            if (newProgress == 100) {
                binding.webLoading.visibility = View.GONE
            } else {
                binding.webLoading.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?, ): Boolean {
            return shouldOverrideUrlLoading(view, request?.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    private fun getWebViewUrl(): String {
        if (intent.extras == null){
            return TiyendeConfigs.TIYENDE_PRIVACY_POLICY_LINK
        }
        return intent.extras?.getString(INTENT_LINK_URL).toString()
    }

    private fun getWebViewTitle(): String {
        if (intent.extras == null){
            return getString(R.string.tiyende_privacy_policy_title)
        }
        return intent.extras?.getString(INTENT_LINK_TITLE).toString()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_WEBVIEW_LINK_START,"${getWebViewTitle()} Page")
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_WEBVIEW_LINK_END,"${getWebViewTitle()} Page")
    }
}